/**
 * Copyright (C) 2010-2013 Alibaba Group Holding Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.zzw.rocketmq;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.alibaba.rocketmq.client.exception.MQBrokerException;
import com.alibaba.rocketmq.client.exception.MQClientException;
import com.alibaba.rocketmq.client.producer.DefaultMQProducer;
import com.alibaba.rocketmq.client.producer.MessageQueueSelector;
import com.alibaba.rocketmq.client.producer.SendResult;
import com.alibaba.rocketmq.common.message.Message;
import com.alibaba.rocketmq.common.message.MessageQueue;
import com.alibaba.rocketmq.remoting.exception.RemotingException;

/**
 * Producer，发送顺序消息
 */
public class Producer {
	public static final String TOPIC = "TopicTaobao";

	public static String getNowStr() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(new Date());
	}

	public static void main(String[] args) {
		try {
			DefaultMQProducer producer = new DefaultMQProducer(
					"TopicTestTaobao-producer");
			producer.setNamesrvAddr("10.230.20.224:8765");
			// producer.setNamesrvAddr("localhost:9876");

			producer.setVipChannelEnabled(false);
			producer.start();

			String[] tags = new String[] { "TagA", "TagB", "TagC", "TagD",
					"TagE" };

			String[] orderlys = new String[] { "订单创建", "订单支付", "订单发货", "订单签收",
					"订单完成", "订单评价" };

			for (int step = 0; step < orderlys.length; step++) {

				for (int i = 0; i < 10; i++) {
					Message msg = new Message(TOPIC, tags[i % tags.length],
							"KEY" + i,
							("[" + getNowStr() + "] orderly RocketMQ " + step
									+ "[" + orderlys[step] + "-订单号" + i + "]")
									.getBytes());
					// SelectMessageQueueByHash hash=new
					// SelectMessageQueueByHash();
					SendResult sendResult = producer.send(msg,
							new MessageQueueSelector() {
								@Override
								public MessageQueue select(
										List<MessageQueue> mqs, Message msg,
										Object arg) {
									Integer id = (Integer) arg;
									int index = id % mqs.size();
									return mqs.get(index);
								}
							},
							// hash,
							// orderId
							step);
					System.out.println(sendResult);
				}

			}

			producer.shutdown();
		} catch (MQClientException e) {
			e.printStackTrace();
		} catch (RemotingException e) {
			e.printStackTrace();
		} catch (MQBrokerException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
